package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayTemperature;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayTemperatureMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayTemperatureParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayTemperatureService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 网关温度 服务实现类
 *
 * @author ssy
 * @date 2022/08/12 16:00
 */
@Service
public class BizGatewayTemperatureServiceImpl extends ServiceImpl<BizGatewayTemperatureMapper, BizGatewayTemperature> implements BizGatewayTemperatureService {

    @Override
    public PageResult<Map<String,Object>> pageList(BizGatewayTemperatureParam bizGatewayTemperatureParam) {

        Page<Map<String, Object>> mapPage = PageFactory.defaultPage();
        String gatewayIds = bizGatewayTemperatureParam.getGatewayIds();
        if(StringUtils.isNotBlank(gatewayIds)){
            final String[] split = gatewayIds.split(",");
            mapPage = this.baseMapper.pageList(PageFactory.defaultPage(), bizGatewayTemperatureParam, split);
        }
        // 查询分页结果
        return new PageResult<>(mapPage);
    }

    @Override
    public List<BizGatewayTemperature> list(BizGatewayTemperatureParam bizGatewayTemperatureParam) {

        // 构造条件
        LambdaQueryWrapper<BizGatewayTemperature> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(BizGatewayTemperatureParam bizGatewayTemperatureParam) {

        // 将dto转为实体
        BizGatewayTemperature bizGatewayTemperature = new BizGatewayTemperature();
        BeanUtil.copyProperties(bizGatewayTemperatureParam, bizGatewayTemperature);

        this.save(bizGatewayTemperature);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        this.removeById(bizGatewayTemperatureParam.getId());
    }

    @Override
    public void edit(BizGatewayTemperatureParam bizGatewayTemperatureParam) {

        // 根据id查询实体
        BizGatewayTemperature bizGatewayTemperature = this.queryBizGatewayTemperature(bizGatewayTemperatureParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayTemperatureParam, bizGatewayTemperature);

        this.updateById(bizGatewayTemperature);
    }

    @Override
    public BizGatewayTemperature detail(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        return this.queryBizGatewayTemperature(bizGatewayTemperatureParam);
    }

    @Override
    public List<Integer> listByBeforeTime() {
        return this.baseMapper.listByBeforeTime();
    }


    /**
     * 获取网关温度
     *
     * @author ssy
     * @since 2022/08/12 16:00
     */
    private BizGatewayTemperature queryBizGatewayTemperature(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        BizGatewayTemperature bizGatewayTemperature = this.getById(bizGatewayTemperatureParam.getId());
        return bizGatewayTemperature;
    }

    /**
     * echart查询时间段的温度
     *
     * @author rdp
     * @date 2022/11/02 09:58
     */
    @Override
    public List<BizGatewayTemperature> getTemperatureByDate(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        List<BizGatewayTemperature> temperatureList = this.baseMapper.getTemperatureByDate(bizGatewayTemperatureParam);
        Iterator<BizGatewayTemperature> iterator = temperatureList.iterator();
        int ctn  = 0;
        while (iterator.hasNext()) {
            if (ctn++ % 4 != 0) {
                iterator.remove();
            }
            iterator.next();
        }
        return temperatureList;
    }
}
